﻿using msBsc.AppLogic.DataAlc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.wMall.Seller
{
    public partial class wfOrderList : msBsc.Cls.BPDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (CBizCustomerViews customer=new CBizCustomerViews ())
                {
                    customer.AddCustomerViewRecord("", "", "", msBsc.SvRef.UserInfo.Operator,"","","");
                }
            }
        }

        private void getOrderList()
        {
            int pageIndex = 1;
            int pageSize = 10;
            int.TryParse(this.GetParamHtValue("pIndex"), out pageIndex);
            string solderid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(solderid))
            {
                return;
            }
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.alert('登录失效请重新登录.', 3);");
                return;
            }
            int orderStatus = 0;
            int.TryParse(this.GetParamHtValue("orderStatus"), out orderStatus);

            using (msBsc.AppLogic.DataAlc.CBizOrdersInfo ordersInfo = new AppLogic.DataAlc.CBizOrdersInfo())
            {
                DataSet ds = ordersInfo.GetOrderList("", solderid, (OrderStatusEnum)orderStatus, pageIndex, pageSize);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {

                    this.ExecJScript("$('#orderList').html(\"<p style='width: 200px; margin-left: 30px;' class='car-price'>没有查询到任何相关信息...</p>\");$('#divPage').html('');");
                    return;
                }

                //获取订单各项数量
                DataTable dt = ordersInfo.GetDjkAllSelectCount(userid);
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 5, "");
                this.ExecJScript(@"setAllCount(" + retJson + ");");

                //获取数据
                retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 20, "");
                this.ExecJScript(@"
                            var _html = $('#svc_list_temp_order').html();
                            laytpl(_html).render(" + retJson + @", function (render) {
                                $('#orderList').html(render);
                            });jsPage('divPage', " + ds.Tables[0].Rows[0]["DATACOUNT"] + ", " + pageSize + ", " + pageIndex + ", 'goPage');");
            }
        }
        //拒绝 订单
        public void Refuse()
        {
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.alert('登录失效请重新登录.', 3);");
                return;
            }
            string orderid = this.GetParamHtValue("orderId");
            if (string.IsNullOrEmpty(orderid))
            {
                this.ExecJScript(@"layer.alert('订单ID错误,请重新操作.', 3);");
                return;
            }
            string message = this.GetParamHtValue("message");
            if (string.IsNullOrEmpty(message))
            {
                this.ExecJScript(@"layer.alert('请输入拒绝理由', 3);");
                return;
            }

            //获取订单状态是否存在
            using (msBsc.AppLogic.DataAlc.CBizOrdersInfo orderinfo = new AppLogic.DataAlc.CBizOrdersInfo())
            {
                DataSet dsOrderInfo = orderinfo.GetOrderInfoByUserAndOrderId(orderid, userid);
                if (dsOrderInfo == null || dsOrderInfo.Tables[0].Rows.Count < 1)
                {
                    this.ExecJScript(@"layer.alert('该订单不存在', 3);");
                    return;
                }

                //设置拒绝理由
                dsOrderInfo.Tables[0].Rows[0]["REFUSE_MESSAGE"] = message;
                //修改订单状态 拒绝订单
                dsOrderInfo.Tables[0].Rows[0]["ORDER_STATUS"] = 12;

                orderinfo.Update(dsOrderInfo);

                //修改拒绝状态
                this.ExecJScript(@"success();");
            }
        }
        //删除订单
        public void DeleteOrder()
        {
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.alert('登录失效请重新登录.', 3);");
                return;
            }
            string orderid = this.GetParamHtValue("orderId");
            if (string.IsNullOrEmpty(orderid))
            {
                this.ExecJScript(@"layer.alert('订单ID错误,请重新操作.', 3);");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizOrdersInfo orderinfo = new AppLogic.DataAlc.CBizOrdersInfo())
            {
                string msgs = orderinfo.DeleteOrder(userid, orderid, true);
                if (msgs != "OK")
                {
                    this.ExecJScript(@"layer.alert('" + msgs + "', 3);");
                }
                else
                {
                    this.ExecJScript(@"success();");
                }
            }


        }

        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                //初始化
                getOrderList();
            }
            if (this.strAction == "refuse")
            {
                //拒绝
                Refuse();
            }
            if (this.strAction == "del")
            {
                DeleteOrder();
            }
        }
    }
}